package cn.dao.Servlet;

import cn.dao.dao.BillDao;
import cn.dao.entity.Provider;
import cn.dao.entity.bill;
import cn.dao.util.FreemarkerUtil;
import com.google.gson.Gson;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunbin on 2017/7/14.
 */
public class BillServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        FreemarkerUtil free=new FreemarkerUtil();
        String path= "mybatis-config.xml";
        InputStream is= Resources.getResourceAsStream(path);
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
        SqlSession session=factory.openSession();
        response.setCharacterEncoding("utf-8");
        if("bill".equals(request.getParameter("action"))){
            BillDao bill = session.getMapper(BillDao.class);
            int count=bill.count(null,null,null)%3==0?bill.count(null,null,null)/3:bill.count(null,null,null)/3+1;
            System.out.println(count);
            List<Provider>listg=bill.listg();
                 int index=1;
            List<bill> list=bill.list(0,3,null,null,null);
            for (bill item:list
                 ) {
                if(item.getIsPayment()==1){
                    item.setP("已付款");

                }else {
                    item.setP("未付款");
                }
                item.setData(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(item.getCreationDate())) ;

            }
            Template template = free.getTemplate("billList.html", getServletContext());
            Map<String ,Object> map=new HashMap<String, Object>();
            map.put("user",request.getSession().getAttribute("user"));
            map.put("list",list);
            map.put("listg",listg);
            map.put("index",index);
            map.put("count",count);
            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }

        }else if("cha".equals(request.getParameter("action"))){

            String name=request.getParameter("name");
            String tigong =request.getParameter("tigong");
            Integer fukuan=Integer.parseInt(request.getParameter("fukuan"));
            BillDao bill = session.getMapper(BillDao.class);
            List<Provider>listg=bill.listg();
            if(tigong.equals("0")){
                System.out.println(tigong);
                tigong=null;
            }
            if(name.equals("")){
                name=null;
            }
            if(fukuan==3){
                fukuan=null;
            }
            int index=1;
            int count=bill.count(name,tigong,fukuan)%3==0?bill.count(name,tigong,fukuan)/3:bill.count(name,tigong,fukuan)/3+1;
            System.out.println(count);
            List<bill> list=bill.list(0,3,name,tigong,fukuan);
            for (bill item:list
                    ) {
                if(item.getIsPayment()==1){
                    item.setP("已付款");

                }else {
                    item.setP("未付款");
                }
                item.setCount(count);
                item.setData(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(item.getCreationDate())) ;

            }


            Gson gson=new Gson();
            String li=gson.toJson(list);
            // request.setAttribute("li", li);

            //JSONArray jso n=JSONArray.fromObject(object)js
            PrintWriter out=response.getWriter();

            out.write(li);


        }else if("fenye".equals(request.getParameter("action"))){
            String name=request.getParameter("name");
            String tigong =request.getParameter("tigong");
            Integer fukuan=Integer.parseInt(request.getParameter("fukuan"));
            BillDao bill = session.getMapper(BillDao.class);
            List<Provider>listg=bill.listg();
            if(tigong.equals("0")){
                System.out.println(tigong);
                tigong=null;
            }
            if(name.equals("")){
                name=null;
            }
            if(fukuan==3){
                fukuan=null;
            }
            Integer index=Integer.parseInt(request.getParameter("index"));
            int index2=(index-1)*3;
            List<bill> list=bill.list(index2,3,name,tigong,fukuan);
            for (bill item:list
                    ) {
                if(item.getIsPayment()==1){
                    item.setP("已付款");

                }else {
                    item.setP("未付款");
                }

                item.setData(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(item.getCreationDate())) ;

            }

            Gson gson=new Gson();
            String li=gson.toJson(list);
            // request.setAttribute("li", li);

            //JSONArray jso n=JSONArray.fromObject(object)js
            PrintWriter out=response.getWriter();

            out.write(li);
        }else if("select".equals(request.getParameter("action"))){
                      int id=Integer.parseInt(request.getParameter("id"));
            BillDao bill = session.getMapper(BillDao.class);
            cn.dao.entity.bill bill1 = bill.BillView(id);
            if(bill1.getIsPayment()==1){
                bill1.setP("已付款");
            }else {
                bill1.setP("未付款");
            }
            List<Provider>listg=bill.listg();
            Template template = free.getTemplate("billView.html", getServletContext());
            Map<String ,Object> map=new HashMap<String, Object>();
            map.put("user",request.getSession().getAttribute("user"));
             map.put("list",bill1);
             map.put("listg",listg);
            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }

        }else if("selec".equals(request.getParameter("action"))){
            int id=Integer.parseInt(request.getParameter("id"));
            BillDao bill = session.getMapper(BillDao.class);
            cn.dao.entity.bill bill1 = bill.BillView(id);
            if(bill1.getIsPayment()==1){
                bill1.setP("已付款");
            }else {
                bill1.setP("未付款");
            }
            Template template = free.getTemplate("billUpdate.html", getServletContext());
            Map<String ,Object> map=new HashMap<String, Object>();
            List<Provider>listg=bill.listg();
            map.put("user",request.getSession().getAttribute("user"));
            map.put("list",bill1);
            map.put("listg",listg);
            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }

        }else if("update".equals(request.getParameter("action"))){
             String bh=request.getParameter("bh");
             String name=request.getParameter("name");
             String people=request.getParameter("people");
             int num=Integer.parseInt(request.getParameter("phone"));
             double money=Double.parseDouble(request.getParameter("money"));
             String i=request.getParameter("is");
            System.out.println(i);
             int isn=0;
             if(i.equals("已付款")){
                 isn=1;
             }
            BillDao bill = session.getMapper(BillDao.class);
             bill bill1=new bill();
             bill1.setIsPayment(isn);
             bill1.setProductName(name);
             bill1.setId(Integer.parseInt(request.getParameter("id")));
             bill1.setBillCode(bh);
             bill1.setProductCount(num);
             bill1.setProductUnit(people);
             bill1.setProvider(Integer.parseInt(request.getParameter("fax")));
             bill1.setTotalPrice(money);
             int numm=bill.updates(bill1);
              session.commit();
              session.close();
            Gson gson=new Gson();
            String li=gson.toJson(numm);
            // request.setAttribute("li", li);

            //JSONArray jso n=JSONArray.fromObject(object)js
            PrintWriter out=response.getWriter();

            out.write(li);


        }else if("insert".equals(request.getParameter("action"))){
            BillDao bill = session.getMapper(BillDao.class);
            List<Provider>listg=bill.listg();

            Template template = free.getTemplate("billAdd.html", getServletContext());
            Map<String ,Object> map=new HashMap<String, Object>();
            map.put("user",request.getSession().getAttribute("user"));

            map.put("listg",listg);
            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }

        }else if("inserts".equals(request.getParameter("action"))){
            String bh=request.getParameter("bh");
            String name=request.getParameter("name");
            String people=request.getParameter("people");
            int num=Integer.parseInt(request.getParameter("phone"));
            double money=Double.parseDouble(request.getParameter("money"));
            String i=request.getParameter("is");
            System.out.println(i);
            int isn=0;
            if(i.equals("已付款")){
                isn=1;
            }
            BillDao bill = session.getMapper(BillDao.class);
            cn.dao.entity.bill bill1=new bill();
            bill1.setIsPayment(isn);
            bill1.setProductName(name);
            bill1.setCreationDate(new Date());
            bill1.setBillCode(bh);
            bill1.setProductCount(num);
            bill1.setProductUnit(people);
            bill1.setProvider(Integer.parseInt(request.getParameter("fax")));
            bill1.setTotalPrice(money);
            int nums=bill.inserts(bill1);
            session.commit();
            session.close();
            Gson gson=new Gson();
            String li=gson.toJson(nums);
            // request.setAttribute("li", li);

            //JSONArray jso n=JSONArray.fromObject(object)js
            PrintWriter out=response.getWriter();

            out.write(li);

        }else if("delete".equals(request.getParameter("action"))){

            int id=Integer.parseInt(request.getParameter("id"));
            BillDao bill = session.getMapper(BillDao.class);
            int num=bill.deletes(id);
            session.commit();
            Gson gson=new Gson();
            String li=gson.toJson(num);
            // request.setAttribute("li", li);

            //JSONArray jso n=JSONArray.fromObject(object)js
            PrintWriter out=response.getWriter();

            out.write(li);

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}
